package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.dto.ChannelTradeAmountRandomRuleDTO;
import com.ayf.payment.game.api.entity.ChannelTradeAmountRandomRule;
import com.ayf.payment.game.api.mapper.ChannelTradeAmountRandomRuleMapper;
import com.ayf.payment.game.api.service.ChannelTradeAmountRandomRuleService;
import com.ayf.payment.game.api.utils.BigDecimalUtil;
import com.ayf.payment.game.api.utils.DateUtils;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class ChannelTradeAmountRandomRuleServiceImpl implements ChannelTradeAmountRandomRuleService{

    @Resource
    private ChannelTradeAmountRandomRuleMapper channelTradeAmountRandomRuleMapper;

    @Override
    public List<ChannelTradeAmountRandomRuleDTO> getChannelTradeAmountRandomRuleList(ChannelTradeAmountRandomRuleDTO channelTradeAmountRandomRuleDTO) {
        return channelTradeAmountRandomRuleMapper.getChannelTradeAmountRandomRuleList(channelTradeAmountRandomRuleDTO);
    }

    @Override
    public void saveAndUpChannelTradeAmtRule(ChannelTradeAmountRandomRuleDTO channelTradeAmountRandomRuleDTO) {
        if (channelTradeAmountRandomRuleDTO.getId() == null){
            Date currentDate = DateUtils.getNowDate();
            channelTradeAmountRandomRuleDTO.setCreateTime(currentDate);
            channelTradeAmountRandomRuleDTO.setLastUpdateTime(currentDate);
            channelTradeAmountRandomRuleMapper.insert(BeanConvertUtils.convert(channelTradeAmountRandomRuleDTO,ChannelTradeAmountRandomRule.class));
        } else {
            Date currentDate = DateUtils.getNowDate();
            channelTradeAmountRandomRuleDTO.setLastUpdateTime(currentDate);
            channelTradeAmountRandomRuleMapper.updateByPrimaryKeySelective(BeanConvertUtils.convert(channelTradeAmountRandomRuleDTO,ChannelTradeAmountRandomRule.class));
        }
    }

    @Override
    public void deleteChannelTradeAmtRuleById(Integer id) {
        ChannelTradeAmountRandomRule channelTradeAmountRandomRule = new ChannelTradeAmountRandomRule();
        channelTradeAmountRandomRule.setId(id);
        channelTradeAmountRandomRuleMapper.delete(channelTradeAmountRandomRule);
    }

    @Override
    public BigDecimal getChannelTradeAmountRandomRule(Integer payChannelId, BigDecimal checkAmt) {
        BigDecimal randomAmt = BigDecimal.valueOf(0.00);
        ChannelTradeAmountRandomRule channelTradeAmountRandomRule = channelTradeAmountRandomRuleMapper.getChannelTradeAmountRandomRule(payChannelId,checkAmt);
        if (Objects.isNull(channelTradeAmountRandomRule)){
            return randomAmt;
        }
        try {
            randomAmt = BigDecimalUtil.randomAmt(channelTradeAmountRandomRule.getRandomMinAmt(),channelTradeAmountRandomRule.getRandomMaxAmt());
            LogPortal.info("渠道ID:{} 充值金额:{} 获取随机数金额:{}",payChannelId,checkAmt,randomAmt);
        } catch (Exception e) {
            LogPortal.error("渠道ID:{} 充值金额:{} 获取随机数金额发生{}异常",e,payChannelId,checkAmt,e.getClass().getName());
        }
        return randomAmt;
    }
}
